﻿//*****************************字符串对象的增强方法************************************

//escape不编码字符有69个：[*] [+] [-] [.] [/] [@] [_] [0-9] [a-z] [A-Z]
//encodeURI不编码字符有82个：[!] [#] [$] [&] ['] [(] [)] [*] [+] [,] [-] [.] [/] [:] [;] [=] [?] [@] [_] [~] [0-9] [a-z] [A-Z]
//encodeURIComponent不编码字符有71个：[!] ['] [(] [)] [*] [-] [.] [_] [~] [0-9] [a-z] [A-Z]
//String.prototype.escape = function () {
//    return escape(this);
//};

var MyString = {
    has: function (sStr, sSubStr) {
        /// <summary>判断指定字符串中是否含有指定的子字符串</summary>
        /// <param name="sStr" type="String">查找的目标字符串</param>
        /// <param name="sSubStr" type="String">需要查找的子字符串</param>
        return sStr.indexOf(sSubStr) !== -1;
    },
    hasSubString: function (sStr, sSubStr, sDivStr, bIgnoreCase) {
        /// <summary>判断指定字符串中是否含有指定的子字符串, 可指定是否区分大小写</summary>
        /// <param name="sStr" type="String">查找的目标字符串</param>
        /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号</param>
        /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
        return MyString.contains(sStr, sSubStr, sDivStr, bIgnoreCase);
    },
    contains: function (sStr, sSubStr, sDiv, bIgnoreCase) {
        /// <summary>比较子字符串是否存在, 可指定是否区分大小写，并且还可以判断是否需要分隔符</summary>
        /// <param name="sStr" type="String">查找的目标字符串</param>
        /// <param name="sSubStr" type="String">需要查找的子字符串</param>
        /// <param name="sDiv" type="String">字符串的分隔符</param>
        /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
        if (sDiv) {
            var sStr = sDiv + sStr + sDiv;
            sSubStr = sDiv + sSubStr + sDiv;
            //大小写不敏感
            if (bIgnoreCase) {
                return sStr.toLowerCase().indexOf(sSubStr.toLowerCase()) >= 0;
            }
            else {
                //大小写敏感
                return sStr.indexOf(sSubStr) >= 0;
            }
        }
        else {
            return sStr.indexOf(sSubStr) >= 0;
        }
    },
    append: function (sStr, sSubStr, sDivChar) {
        /// <summary>往字符串中追加内容</summary>
        /// <param name="sStr" type="String">追加到的目标字符串</param>
        /// <param name="sSubStr" type="String">需要追加的子字符串</param>
        /// <param name="sDivStr" type="String">原内容与追加内容之间的分隔符号（默认为分号;）</param>
        if (!sDivChar) sDivChar = ";";
        if (!sStr) return sSubStr;
        else return sStr + sDivChar + sSubStr;
    },
    removeRepeatSubString: function (sStr, sDivChar) {
        /// <summary>删除重复的子字符串</summary>
        /// <param name="sStr" type="String">查找的目标字符串</param>
        /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号（默认为分号;）</param>
        if (!sDivChar) sDivChar = ";";
        var sReturn = "";
        var aSubStrs = sStr.split(sDivChar);
        for (var i = 0; i < aSubStrs.length; i++) {
            if (sReturn.hasSubString(aSubStrs[i], sDivChar) == false)
                sReturn = sReturn.append(aSubStrs[i], sDivChar);
        }
        return sReturn;
    },
    removeEmptySubString: function (sStr, sDivChar) {
        /// <summary>删除空的子字符串</summary>
        /// <param name="sStr" type="String">需要删除的目标字符串</param>
        /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号（默认为分号;）</param>
        if (!sDivChar) sDivChar = ";";
        var sReturn = "";
        var aSubStrs = sStr.split(sDivChar);
        for (var i = 0; i < aSubStrs.length; i++) {
            if (aSubStrs[i] == "") continue;
            sReturn = sReturn.append(aSubStrs[i], sDivChar);
        }
        return sReturn;
    },
    removeSubString: function (sStr, sSubStr, sDivChar) {
        /// <summary>从字符串中删除指定的内容</summary>
        /// <param name="sStr" type="String">需要删除的目标字符串</param>
        /// <param name="sSubStr" type="String">要删除的子字符串</param>
        /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号</param>
        if (!sDivChar) sDivChar = ";";
        var sReturn = sDivChar + sStr + sDivChar;
        sReturn = sReturn.replaceAll(sDivChar + sSubStr + sDivChar, sDivChar);
        while (sReturn.indexOf(sDivChar + sDivChar) != -1) {
            sReturn = sReturn.replaceAll(sDivChar + sDivChar, sDivChar);
        }
        if (sReturn.startsWith(sDivChar)) {
            sReturn = sReturn.substr(sDivChar.length);
        }
        if (sReturn.endsWith(sDivChar)) {
            sReturn = sReturn.substr(0, sReturn.length - sDivChar.length);
        }
        return sReturn;
    },
    lengthB: function (sStr) {
        /// <summary>获取字符串的真实宽度（多字节符(汉字、全角符等)，按2个字符计算）</summary>
        return sStr.replace(/[^x00-xff]/g, "**").length;
    },
    hasChineseChar: function (sStr) {
        /// <summary>判断字符串中是否有汉字</summary>
        return /[^\x00-\xff]/g.test(sStr)
    },
    replaceAll: function (sStr, sStrA, sStrB) {
        /// <summary>将字符串中所有的sStrA替换成sStrB</summary>
        /// <param name="sStr" type="String">需要替换的字符串</param>
        if (!sStrA) return sStr;
        while (sStr.indexOf(sStrA) != -1) {
            sStr = sStr.replace(sStrA, sStrB);
        }
        return sStr;
    },
    removeAllNewLine: function (sStr) {
        /// <summary>去掉所有换行符</summary>
        return sStr.replaceAll("(\n|\r|(\r\n)|(\u0085)|(\u2028)|(\u2029))", "");
    },
    format: function (sStr) {
        /// <summary>模拟.NET的格式化字符串</summary>
        if (arguments && arguments.length > 1) {
            for (var i = 1, j = arguments.length; i < j; i++) {
                var oRegex = new RegExp("\\{" + (i-1) + "\\}", "gm");
                sStr = sStr.replace(oRegex, arguments[i]);
            }
        }
        return sStr;
    },
    rightOfLast: function (sStr, sSubStr) {
        /// <summary>取字符串中sSubStr以右的部分(如果字符串中有多个sSubStr, 则指的是最后一个)</summary>
        /// <param name="sStr" type="String">查找的目标字符串</param>
        /// <param name="sSubStr" type="String">标志字符串</param>
        if (sStr.indexOf(sSubStr) == -1)
            return "";
        else
            return sStr.substr(sStr.lastIndexOf(sSubStr) + sSubStr.length);
    },
    rightOf: function (sStr, sSubStr) {
        /// <summary>取字符串中sSubStr以右的部分(如果字符串中有多个sSubStr, 则指的是第一个)</summary>
        /// <param name="sSubStr" type="String">标志字符串</param>
        if (sStr.indexOf(sSubStr) == -1) return "";
        else return sStr.substr(sStr.indexOf(sSubStr) + sSubStr.length);
    },
    leftOfLast: function (sStr, sSubStr) {
        /// <summary>取字符串中sSubStr以左的部分(如果字符串中有多个sSubStr, 则指的是最后一个)</summary>
        /// <param name="sSubStr" type="String">标志字符串</param>
        if (sStr.indexOf(sSubStr) == -1) return "";
        else return sStr.substr(0, sStr.lastIndexOf(sSubStr));
    },
    leftOf: function (sStr, sSubStr) {
        /// <summary>取字符串中sSubStr以左的部分(如果字符串中有多个sSubStr, 则指的是第一个)</summary>
        /// <param name="sSubStr" type="String">标志字符串</param>
        if (sStr.indexOf(sSubStr) == -1) return "";
        else return sStr.substr(0, this.indexOf(sSubStr));
    },
    endsWith: function (sStr, sSubStr, bIgnoreCase) {
        /// <summary>判断字符串是否以某个字段结尾</summary>
        /// <param name="sSubStr" type="String">被判断的子字符串</param>
        /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
        if (sStr.length < sSubStr.length)
            return false;
        var sTemp = sStr.substr(sStr.length - sSubStr.length, sSubStr.length);
        if (bIgnoreCase) {
            if (sTemp.toLowerCase() == sSubStr.toLowerCase())
                return true;
        }
        else {
            if (sTemp == sSubStr)
                return true;
        }
        return false;
    },
    startsWith: function (sStr, sSubStr, bIgnoreCase) {
        /// <summary>判断字符串是否以某个字段开头</summary>
        /// <param name="sSubStr" type="String">被判断的子字符串</param>
        /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
        if (sStr.length < sSubStr.length)
            return false;
        var sTemp = sStr.substr(0, sSubStr.length);
        if (bIgnoreCase) {
            if (sTemp.toLowerCase() == sSubStr.toLowerCase())
                return true;
        }
        else {
            if (sTemp == sSubStr)
                return true;
        }
        return false;
    },
    xmlEncode: function (sStr) {
        /// <summary>Xml特殊字符处理：<转化成&lt; &转化成&amp;</summary>
        return sStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    },
    xmlDecode: function (sStr) {
        /// <summary>Xml特殊字符处理：&lt;转化成< &amp;转化成&</summary>
        return sStr.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
    },
    trim: function (sStr) {
        /// <summary>去掉字符串前后的空格</summary>
        return sStr.replace(/(^[\s]*)|([\s]*$)/g, "");
    },
    leftTrim: function (sStr) {
        /// <summary>去掉字符串左边的空格</summary>
        return sStr.replace(/(^[\s]*)/g, "");
    },
    rightTrim: function (sStr) {
        /// <summary>去掉字符串右边的空格</summary>
        return sStr.replace(/([\s]*$)/g, "");
    },
    left: function (sStr, iCount) {
        /// <summary>截取字符串左边的iCount个字符</summary>
        /// <param name="iCount" type="Int">需要截取的字符长度</param>
        return sStr.substr(0, iCount);
    },
    right: function (sStr, iCount) {
        /// <summary>截取字符串右边的iCount个字符</summary>
        /// <param name="iCount" type="Int">需要截取的字符长度</param>
        return sStr.substr(sStr.length - iCount, iCount);
    },
    urlAppend: function (sUrl, sString) {
        /// <summary>追加url参数</summary>
        /// <param name="sString" type="String">需要追加的url参数</param>
        return sUrl + (sUrl.indexOf('?') === -1 ? '?' : '&') + sString;
    }
};

